<?php

class OOWP_Loop
    extends OOWP_Formattable
{

    public function __construct(array $args = array( ))
    {
        global $wp_query;

        $input = array_merge(array(
            'article_class' => 'OOWP_Element_Article',
            'query' => $wp_query,
        ), $args);

        parent::__construct($input);
        $this->dict['contents'] = new OOWP_FormatSlot($this);
        $this->dict['contents']->extend($this->init_contents( ));
    }

    /* ACCESSORS */

    protected function getArticleClass( )
    {
        return $this->dict['article_class'];
    }

    protected function setArticleClass($class)
    {
        if (is_string($class)) {
            $class = trim($class);
//            if (class_exists($class)) {
                $this->dict['article_class'] = $class;
//            }
        }
    }

    protected function getContents( )
    {
        return $this->dict['contents'];
    }

    protected function getFormatPieces( )
    {
        return $this->collect_format_pieces($this->contents);
    }

    protected function getQuery( )
    {
        return $this->dict['query'];
    }

    protected function setQuery(WP_Query $query)
    {
        $this->dict['query'] = $query;
    }

    /* METHODS */

    protected function init_contents( )
    {
        $result = array( );
        foreach ($this->query->posts as $post) {
            $article_class = $this->article_class;
            $article = new $article_class(array(
                'post' => $post,
            ));
            $result[ ] = $article;
        }
        return $result;
    }


}
